package de.se.ludoapp.activity.task;

import android.graphics.Color;
import android.os.AsyncTask;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import de.se.ludoapp.LudoApplication;
import de.se.ludoapp.R;
import de.se.ludoapp.activity.PlayerTableActivity;
import de.se.ludoapp.gaming.Game;
import de.se.ludoapp.response.ReturncodeResponse;

/**
 * Task to remove request for a game on the server.
 * 
 * @author Marlen Rotert
 * 
 */
public class RemoveRequestForGameTask extends
		AsyncTask<String, Integer, String> {

	private LudoApplication ludoApp;
	private PlayerTableActivity context;
	private Game gameOfRequest;

	public RemoveRequestForGameTask(LudoApplication ludoApp,
			PlayerTableActivity context, Game gameOfRequest) {
		super();
		this.ludoApp = ludoApp;
		this.context = context;
		this.gameOfRequest = gameOfRequest;
	}

	/**
	 * Destroy request on Server.<br>
	 * This method is executed in a concurrent thread (asynchronous). Therefore
	 * you mustn't accessed on view elements!
	 */
	@Override
	protected String doInBackground(String... params) {
		/* Execute remove request */
		ReturncodeResponse response = ludoApp.getLudoStub()
				.removeRequestingPlayerForGame(
						ludoApp.getlogedinUser().getSessionId(),
						gameOfRequest.getGameId());
		return response.getMsg();
	}

	/**
	 * Back to last Activity <br>
	 * This method is invoked when the OpenGameTask is executed. You are only
	 * allowed here to access on view Elements (TextView and Buttons)!
	 */
	@Override
	protected void onPostExecute(String result) {
		Toast.makeText(context, result, Toast.LENGTH_LONG).show();
		Button buttonOpenNewGameTable = (Button) context
				.findViewById(R.id.buttonOpenNewGameTable);
		buttonOpenNewGameTable.setEnabled(true);
		ListView listViewOpenGames = (ListView) context
				.findViewById(R.id.listViewOpenGames);
		listViewOpenGames.setEnabled(true);
		listViewOpenGames.setBackgroundColor(Color.TRANSPARENT);
		new OpenGamesTask(ludoApp, context).execute();
	}
}
